package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.qiniu.SevenUploadUtil;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.StrategyQuery;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private ITravelService travelService;


    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyQuery qo, Model model){
        model.addAttribute("result",strategyService.selectForList(qo));
        model.addAttribute("places",regionService.selectAll());
        return "strategy/list";
    }



    //热点关注,下拉框回显
    @RequestMapping("lookList")
    public String lookList(Long strategyId, Model model){
        model.addAttribute("list",strategyService.selectTravelByStrategyId(strategyId));
        model.addAttribute("strategies",strategyService.selectAll());
        model.addAttribute("strategyId",strategyId);
        return "strategy/lookList";
    }

    //热点关注,下拉框回显
    @RequestMapping("travelList")
    @ResponseBody
    public Object travelList(Long strategyId){
        List<Travel> travels = strategyService.selectTravelByStrategyId(strategyId);
        return travels;
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Strategy strategy, MultipartFile file){
        JSONResult result = new JSONResult();
        try {
            if(file != null && file.getSize() > 0){
                String imgUrl = SevenUploadUtil.uploadQiniu(file);
                strategy.setCoverUrl(imgUrl);
            }
            strategyService.saveOrUpdate(strategy);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("deleteRelation")
    public String deleteRelation(Long travelId,Long strategyId){
        travelService.deleteRealtion(travelId, strategyId);
        return "redirect:/strategy/lookList.do?strategyId=" + strategyId;
    }

    @RequestMapping("insertRelation")
    @ResponseBody
    public JSONResult insertRelation(Long strategyId, Long travelId){
        JSONResult result = new JSONResult();
        try {
            travelService.insertRelation(strategyId, travelId);
        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }
}
